/*
 * Copyright (c) 2010-2024. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.serialization.avro.test;

import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.SchemaStore;
import org.apache.avro.specific.SpecificData;

/**
 * Autogenerated by Avro maven plugin v1.12.0, copied to test/src to avoid including plugin in this maven build.
 * <p>
 * DO NOT EDIT DIRECTLY
 */
@org.apache.avro.specific.AvroGenerated
public class ComplexObject extends org.apache.avro.specific.SpecificRecordBase
        implements org.apache.avro.specific.SpecificRecord {

    private static final long serialVersionUID = -213207546923932305L;


    public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse(
            "{\"type\":\"record\",\"name\":\"ComplexObject\",\"namespace\":\"org.axonframework.serialization.avro.test\",\"fields\":[{\"name\":\"value1\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"value2\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"},\"default\":\"default value\"},{\"name\":\"value3\",\"type\":\"int\"}]}");

    public static org.apache.avro.Schema getClassSchema() {
        return SCHEMA$;
    }

    private static final SpecificData MODEL$ = new SpecificData();

    private static final BinaryMessageEncoder<ComplexObject> ENCODER =
            new BinaryMessageEncoder<>(MODEL$, SCHEMA$);

    private static final BinaryMessageDecoder<ComplexObject> DECODER =
            new BinaryMessageDecoder<>(MODEL$, SCHEMA$);

    /**
     * Return the BinaryMessageEncoder instance used by this class.
     * @return the message encoder used by this class
     */
    public static BinaryMessageEncoder<ComplexObject> getEncoder() {
        return ENCODER;
    }

    /**
     * Return the BinaryMessageDecoder instance used by this class.
     * @return the message decoder used by this class
     */
    public static BinaryMessageDecoder<ComplexObject> getDecoder() {
        return DECODER;
    }

    /**
     * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
     * @param resolver a {@link SchemaStore} used to find schemas by fingerprint
     * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
     */
    public static BinaryMessageDecoder<ComplexObject> createDecoder(SchemaStore resolver) {
        return new BinaryMessageDecoder<>(MODEL$, SCHEMA$, resolver);
    }

    /**
     * Serializes this ComplexObject to a ByteBuffer.
     * @return a buffer holding the serialized data for this instance
     * @throws java.io.IOException if this instance could not be serialized
     */
    public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
        return ENCODER.encode(this);
    }

    /**
     * Deserializes a ComplexObject from a ByteBuffer.
     * @param b a byte buffer holding serialized data for an instance of this class
     * @return a ComplexObject instance decoded from the given buffer
     * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
     */
    public static ComplexObject fromByteBuffer(
            java.nio.ByteBuffer b) throws java.io.IOException {
        return DECODER.decode(b);
    }

    private String value1;
    private String value2;
    private int value3;

    /**
     * Default constructor.  Note that this does not initialize fields
     * to their default values from the schema.  If that is desired then
     * one should use <code>newBuilder()</code>.
     */
    public ComplexObject() {
    }

    /**
     * All-args constructor.
     * @param value1 The new value for value1
     * @param value2 The new value for value2
     * @param value3 The new value for value3
     */
    public ComplexObject(String value1, String value2, Integer value3) {
        this.value1 = value1;
        this.value2 = value2;
        this.value3 = value3;
    }

    @Override
    public SpecificData getSpecificData() {
        return MODEL$;
    }

    @Override
    public org.apache.avro.Schema getSchema() {
        return SCHEMA$;
    }

    // Used by DatumWriter.  Applications should not call.
    @Override
    public Object get(int field$) {
        switch (field$) {
            case 0:
                return value1;
            case 1:
                return value2;
            case 2:
                return value3;
            default:
                throw new IndexOutOfBoundsException("Invalid index: " + field$);
        }
    }

    // Used by DatumReader.  Applications should not call.
    @Override
    @SuppressWarnings(value = "unchecked")
    public void put(int field$, Object value$) {
        switch (field$) {
            case 0:
                value1 = value$ != null ? value$.toString() : null;
                break;
            case 1:
                value2 = value$ != null ? value$.toString() : null;
                break;
            case 2:
                value3 = (Integer) value$;
                break;
            default:
                throw new IndexOutOfBoundsException("Invalid index: " + field$);
        }
    }

    /**
     * Gets the value of the 'value1' field.
     * @return The value of the 'value1' field.
     */
    public String getValue1() {
        return value1;
    }


    /**
     * Gets the value of the 'value2' field.
     * @return The value of the 'value2' field.
     */
    public String getValue2() {
        return value2;
    }


    /**
     * Gets the value of the 'value3' field.
     * @return The value of the 'value3' field.
     */
    public int getValue3() {
        return value3;
    }


    /**
     * Creates a new ComplexObject RecordBuilder.
     * @return A new ComplexObject RecordBuilder
     */
    public static Builder newBuilder() {
        return new Builder();
    }

    /**
     * Creates a new ComplexObject RecordBuilder by copying an existing Builder.
     * @param other The existing builder to copy.
     * @return A new ComplexObject RecordBuilder
     */
    public static Builder newBuilder(Builder other) {
        if (other == null) {
            return new Builder();
        } else {
            return new Builder(other);
        }
    }

    /**
     * Creates a new ComplexObject RecordBuilder by copying an existing ComplexObject instance.
     * @param other The existing instance to copy.
     * @return A new ComplexObject RecordBuilder
     */
    public static Builder newBuilder(ComplexObject other) {
        if (other == null) {
            return new Builder();
        } else {
            return new Builder(other);
        }
    }

    /**
     * RecordBuilder for ComplexObject instances.
     */
    @org.apache.avro.specific.AvroGenerated
    public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<ComplexObject>
            implements org.apache.avro.data.RecordBuilder<ComplexObject> {

        private String value1;
        private String value2;
        private int value3;

        /** Creates a new Builder */
        private Builder() {
            super(SCHEMA$, MODEL$);
        }

        /**
         * Creates a Builder by copying an existing Builder.
         * @param other The existing Builder to copy.
         */
        private Builder(Builder other) {
            super(other);
            if (isValidValue(fields()[0], other.value1)) {
                this.value1 = data().deepCopy(fields()[0].schema(), other.value1);
                fieldSetFlags()[0] = other.fieldSetFlags()[0];
            }
            if (isValidValue(fields()[1], other.value2)) {
                this.value2 = data().deepCopy(fields()[1].schema(), other.value2);
                fieldSetFlags()[1] = other.fieldSetFlags()[1];
            }
            if (isValidValue(fields()[2], other.value3)) {
                this.value3 = data().deepCopy(fields()[2].schema(), other.value3);
                fieldSetFlags()[2] = other.fieldSetFlags()[2];
            }
        }

        /**
         * Creates a Builder by copying an existing ComplexObject instance
         * @param other The existing instance to copy.
         */
        private Builder(ComplexObject other) {
            super(SCHEMA$, MODEL$);
            if (isValidValue(fields()[0], other.value1)) {
                this.value1 = data().deepCopy(fields()[0].schema(), other.value1);
                fieldSetFlags()[0] = true;
            }
            if (isValidValue(fields()[1], other.value2)) {
                this.value2 = data().deepCopy(fields()[1].schema(), other.value2);
                fieldSetFlags()[1] = true;
            }
            if (isValidValue(fields()[2], other.value3)) {
                this.value3 = data().deepCopy(fields()[2].schema(), other.value3);
                fieldSetFlags()[2] = true;
            }
        }

        /**
         * Gets the value of the 'value1' field.
         * @return The value.
         */
        public String getValue1() {
            return value1;
        }


        /**
         * Sets the value of the 'value1' field.
         * @param value The value of 'value1'.
         * @return This builder.
         */
        public Builder setValue1(String value) {
            validate(fields()[0], value);
            this.value1 = value;
            fieldSetFlags()[0] = true;
            return this;
        }

        /**
         * Checks whether the 'value1' field has been set.
         * @return True if the 'value1' field has been set, false otherwise.
         */
        public boolean hasValue1() {
            return fieldSetFlags()[0];
        }


        /**
         * Clears the value of the 'value1' field.
         * @return This builder.
         */
        public Builder clearValue1() {
            value1 = null;
            fieldSetFlags()[0] = false;
            return this;
        }

        /**
         * Gets the value of the 'value2' field.
         * @return The value.
         */
        public String getValue2() {
            return value2;
        }


        /**
         * Sets the value of the 'value2' field.
         * @param value The value of 'value2'.
         * @return This builder.
         */
        public Builder setValue2(String value) {
            validate(fields()[1], value);
            this.value2 = value;
            fieldSetFlags()[1] = true;
            return this;
        }

        /**
         * Checks whether the 'value2' field has been set.
         * @return True if the 'value2' field has been set, false otherwise.
         */
        public boolean hasValue2() {
            return fieldSetFlags()[1];
        }


        /**
         * Clears the value of the 'value2' field.
         * @return This builder.
         */
        public Builder clearValue2() {
            value2 = null;
            fieldSetFlags()[1] = false;
            return this;
        }

        /**
         * Gets the value of the 'value3' field.
         * @return The value.
         */
        public int getValue3() {
            return value3;
        }


        /**
         * Sets the value of the 'value3' field.
         * @param value The value of 'value3'.
         * @return This builder.
         */
        public Builder setValue3(int value) {
            validate(fields()[2], value);
            this.value3 = value;
            fieldSetFlags()[2] = true;
            return this;
        }

        /**
         * Checks whether the 'value3' field has been set.
         * @return True if the 'value3' field has been set, false otherwise.
         */
        public boolean hasValue3() {
            return fieldSetFlags()[2];
        }


        /**
         * Clears the value of the 'value3' field.
         * @return This builder.
         */
        public Builder clearValue3() {
            fieldSetFlags()[2] = false;
            return this;
        }

        @Override
        @SuppressWarnings("unchecked")
        public ComplexObject build() {
            try {
                ComplexObject record = new ComplexObject();
                record.value1 = fieldSetFlags()[0] ? this.value1 : (String) defaultValue(fields()[0]);
                record.value2 = fieldSetFlags()[1] ? this.value2 : (String) defaultValue(fields()[1]);
                record.value3 = fieldSetFlags()[2] ? this.value3 : (Integer) defaultValue(fields()[2]);
                return record;
            } catch (org.apache.avro.AvroMissingFieldException e) {
                throw e;
            } catch (Exception e) {
                throw new org.apache.avro.AvroRuntimeException(e);
            }
        }
    }

    @SuppressWarnings("unchecked")
    private static final org.apache.avro.io.DatumWriter<ComplexObject>
            WRITER$ = (org.apache.avro.io.DatumWriter<ComplexObject>) MODEL$.createDatumWriter(SCHEMA$);

    @Override
    public void writeExternal(java.io.ObjectOutput out)
            throws java.io.IOException {
        WRITER$.write(this, SpecificData.getEncoder(out));
    }

    @SuppressWarnings("unchecked")
    private static final org.apache.avro.io.DatumReader<ComplexObject>
            READER$ = (org.apache.avro.io.DatumReader<ComplexObject>) MODEL$.createDatumReader(SCHEMA$);

    @Override
    public void readExternal(java.io.ObjectInput in)
            throws java.io.IOException {
        READER$.read(this, SpecificData.getDecoder(in));
    }

    @Override
    protected boolean hasCustomCoders() {
        return true;
    }

    @Override
    public void customEncode(org.apache.avro.io.Encoder out)
            throws java.io.IOException {
        out.writeString(this.value1);

        out.writeString(this.value2);

        out.writeInt(this.value3);
    }

    @Override
    public void customDecode(org.apache.avro.io.ResolvingDecoder in)
            throws java.io.IOException {
        org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
        if (fieldOrder == null) {
            this.value1 = in.readString();

            this.value2 = in.readString();

            this.value3 = in.readInt();
        } else {
            for (int i = 0; i < 3; i++) {
                switch (fieldOrder[i].pos()) {
                    case 0:
                        this.value1 = in.readString();
                        break;

                    case 1:
                        this.value2 = in.readString();
                        break;

                    case 2:
                        this.value3 = in.readInt();
                        break;

                    default:
                        throw new java.io.IOException("Corrupt ResolvingDecoder.");
                }
            }
        }
    }
}










